<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Mar 25, 2012
 * Time: 4:51:08 PM
 */
class UserController extends BaseController {
    function action() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        $userMessage = array();
        $userMessage['type'] = 'error';
        $userMessage['str'] = 'this is User page';
        $this->render('user', array('msg' => $userMessage,));
    }

    function actionRole() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $userMessage = array();
        $userMessage['type'] = 'success';
        $userMessage['str'] = 'This is Role page';
        $this->render('role', array('msg' => $userMessage,));
    }

    function actionPrivilege() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $userMessage = array();
        $userMessage['type'] = 'success';
        $userMessage['str'] = 'This is Role page';
        $this->render('privilege', array('msg' => $userMessage,));
    }

    function actionProfile() {
        $user = new User();
        $theme = $user->actionGetThemeListMenu();
        $profile = $user->getProfile();
        $userMessage = array();
        $userMessage['type'] = 'error';
        $userMessage['str'] = 'this is User page';
        $this->render('profile', array('msg' => $userMessage, 'profile' => $profile, 'theme' => $theme));
    }

    function actionAdd() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        $user = new User();
        $user->fullName = $_POST["fullName"];
        $user->userName = $_POST["uname"]; //user name not change in
        $user->nic = $_POST['nic'];
        $user->password = $_POST['pass1'];
        $user->email = $_POST['email'];
        $user->id = $_POST["id"];
        $user->profile_id = $_POST["profile_id"];
        $user->status = $_POST["status"];
        $i = $user->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new user ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated user information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ";
                $_type = "error";
            }
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionResetPassword() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        $user = new User();
        $user->id = $_POST['uid'];
        $user->password = $_POST['chpass1'];
        $i = $user->resetPassword();

        if ($i == -1) {
            $_message = "No data was changed !";
            $_type = "info";
        } else if ($i == 1) {
            $_message = "Successsfully updated user password ! ";
            $_type = "success";
        } else {
            $_message = " Unsuccesssfuly update ! ";
            $_type = "error";
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);

    }

    function actionPassword() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        $_userId = $_SESSION['user_id'];
        $_password = $_POST['currentpw'];
        $user = new User();
        if ($user->validatePassword($_userId, $_password)) {
            $user->id = $_userId;
            $user->password = $_POST['pass1'];
            $i = $user->resetPassword();
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfully updated your password ! ";
                $_type = "success";
            } else {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        } else {
            $_message = "Current password is wrong. Please enter correct password !";
            $_type = "warning";
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionEdit() {
        if ($this->userPermission["USER.EDIT"] == 0) //check user privilege
            $this->urlRedirect('home');
        $user = new User();
        $user->fullName = $_POST["fullName"];
        $user->initName = $_POST["initName"]; //user name not change in
        $user->nic = $_POST['nic'];
        $user->email = $_POST['email'];
        $user->profile_id = $_SESSION['profile_id'];
        $user->gender = $_POST["gender"];
        $user->birthday = $_POST["birthday"];
        $i = $user->edit();
        if ($i == -1) {
            $_message = "No data was changed !";
            $_type = "info";
        } else if ($i == 1) {
            $_message = "Successsfuly updated your information !";
            $_type = "success";
        } else {
            $_message = " Unsuccesssfuly update !";
            $_type = "error";
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionDelete() {
        if ($this->userPermission["USER.DELETE"] == 0) {
            $_message = "You Don't have permision to do this taks ";
            $_type = "warning";
            $data = array('message' => $_message, 'type' => $_type);
            echo json_encode($data);
            exit();

        }
        $user = new User();
        $user->id = $_POST["id"];
        $user->profile_id = $_POST["profile_id"];
        $i = $user->delete();
        if ($i == -1) {
            $_message = "No data was changed";
            $_type = "info";
        } else if ($i == 1) {
            $_message = " Successfully Deleteed User ";
            $_type = "success";
        } else if ($i == 0) {
            $_message = " Unsuccesssfuly delete user ";
            $_type = "error";
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddRole() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $role = new Role();
        $role->roleName = $_POST["roleName"];
        $role->status = $_POST["status"];
        $role->id = $_POST["id"];
        $i = $role->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new role ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated role information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update !";
                $_type = "error";
            }
        }

        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
        //echo "status i=". $i."  .....". $_POST["fullName"];
    }

    function actionAddPrivilege() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $privilege = new PrivilegeCode();
        $privilege->privilegeName = $_POST["privilegeName"];
        $privilege->privilegeDes = $_POST["privilegeDes"];
        $privilege->status = $_POST["status"];
        $privilege->id = $_POST["id"];
        $i = $privilege->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new privilege ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated privilege information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionRoleMap() {
        if ($this->userPermission["MENU.USER.ROLE.PRIVILEGE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $this->render('roleMap');

    }

    function actionUserMap() {
        if ($this->userPermission["MENU.USER.ROLE.MAP"] == 0) //check user privilege
            $this->urlRedirect('home');
        $this->render('userMap');

    }

    public function actionGridUser() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["userName"]))
            $userName = $_GET['userName'];
        else
            $userName = "";
        if (isset($_GET["name"]))
            $name = $_GET['name'];
        else
            $name = "";
        if (isset($_GET["nic"]))
            $nic = $_GET['nic'];
        else
            $nic = "";
        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("user_name", "full_name", "nic");

        $user = new User();
        $xml = $user->getGrid($posStart, $count, $userName, $name, $nic, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }

    public function actionGridPrivilege() {
        /* if($this->userPermission["MENU.USER"]==0)  //check user privilege
      $this->urlRedirect('home');  */
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["privilegeName"]))
            $name = $_GET['privilegeName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "code", "status");

        $privilege = new PrivilegeCode();
        $xml = $privilege->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }

    public function actionGridRole() {
        if ($this->userPermission["MENU.USER"] == 0) //check user privilege
            $this->urlRedirect('home');
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["roleName"]))
            $name = $_GET['roleName'];
        else
            $name = "";
        if (isset($_GET["status"]))
            $status = $_GET['status'];

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "name", "status");

        $role = new Role();
        $xml = $role->getGrid($posStart, $count, $name, $status, $fields[$_GET["orderBy"]], $_GET["direction"]);
        header("Content-type:text/xml");
        print $xml;

    }

    public function actionGetRolePrivilege() {
        $privilege = new PrivilegeCode();
        $role_id = $_POST["id"];
        $assignPrivilege = $privilege->getPrivilege($role_id);
        $availablePrivilege = $privilege->getNotPrivilege($role_id);
        echo json_encode(array($availablePrivilege, $assignPrivilege));
    }

    public function actionGetUserRole() {
        $role = new Role();
        $user_id = $_POST["id"];
        $assignRole = $role->getRole($user_id);
        $availableRole = $role->getNotRole($user_id);
        echo json_encode(array($availableRole, $assignRole));
    }

    public function actionTest() {
        $new = array('1', '2', '3', '5');
        $old = array('1', '2', '5', 8, 7, 3);

        $add = array_diff($new, $old);
        $delete = array_diff($old, $new);
        /*
         $a= array_values($add);
         print_r ($new) ;
          echo "<hr>"   ;
          print_r ($old) ;
          echo "<hr>"   ;
         echo "new Array ................   " ; print_r ($a) ;
          echo "<hr>"   ;
          echo "delete Array...............    " ;print_r ($delete) ;  echo "<hr>"   ;
        */
        $privilege = new PrivilegeCode();
        //  $add = array('1', '2',5);
        //  $delete = array('1', '2', '5' );

        $d = $privilege->assignRoleToPrivilege($add, $delete, 3);

        if (!$d['del']) {
            print 'empty delete error' . '<br>';
        } else {
            echo 'Success! Delete';
            echo "<br/>";
        }

        if (!$d['add']) {
            print 'empty add error' . '<br>';
        } else {
            echo 'Success! Added';
            echo "<br/>";
        }

    }

    public function actionRolePrivilege() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');

        $roleId = $_POST["id"];
        $assignPrivilegeNew = $_POST["assignPrivilege"];
        $privilege = new PrivilegeCode();
        $assignPrivilegeOld = array_values($privilege->getPrivilege($roleId, false));
        $addPrivilegeId = array_diff($assignPrivilegeNew, $assignPrivilegeOld);
        $deletePrivilegeId = array_diff($assignPrivilegeOld, $assignPrivilegeNew);

        $d = $privilege->assignRoleToPrivilege($addPrivilegeId, $deletePrivilegeId, $roleId);


        if ($d['delStatus'] & $d['Status']) {

        }

        $_message .= 'Successsfully updated';
        $_type = "success";


        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
        // echo "role id is : $roleId................" .print_r($assignPrivilegeOld )  ;
    }

    public function actionUserRole() {
        if ($this->userPermission["MENU.USER.ROLE"] == 0) //check user privilege
            $this->urlRedirect('home');

        $userId = $_POST["id"];
        $assignRoleNew = $_POST["assignRole"];
        $role = new Role();
        $assignRoleOld = array_values($role->getRole($userId, false));
        $addRoleId = array_diff($assignRoleNew, $assignRoleOld);
        $deleteRoleId = array_diff($assignRoleOld, $assignRoleNew);

        $d = $role->assignUserToRole($addRoleId, $deleteRoleId, $userId);


        if ($d['delStatus'] & $d['Status']) {

        }

        $_message .= 'Successsfully updated';
        $_type = "success";


        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
        // echo "role id is : $roleId................" .print_r($assignPrivilegeOld )  ;
    }

    public function actionTest2() {
        $privilege = new PrivilegeCode();
        $assignPrivilegeOld = array_values($privilege->getPrivilege(3, false));
        // $assignPrivilege=$privilege->getPrivilege(3);
        print_r($assignPrivilegeOld);
    }
}


 
